/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rafkind.raytracer;

import java.util.Arrays;

/**
 *
 * @author drafkind
 */
public class Vector3 {
  private double[] data;

  public Vector3(String d) {
    String[] sp = d.split(",");
    data = new double[sp.length];
    for (int i = 0; i < sp.length; i++)
      data[i] = Double.parseDouble(sp[i]);
  }

  public Vector3(double ...d) {
    data = d;
  }

  public Vector3() {
    data = new double[3];
  }

  public Vector3(Vector3 other) {
    if (other.getData() != null) {
      data = Arrays.copyOf(other.getData(), other.getData().length);
    }
  }

  public double[] getData() {
    return data;
  }

  public Vector3 minus(Vector3 other) {
    double[] d = new double[this.data.length];
    for (int i = 0; i < d.length; i++) {
      d[i] = this.data[i] - other.data[i];
    }

    return new Vector3(d);
  }

  public Vector3 plus(Vector3 other) {
    double[] d = new double[this.data.length];
    for (int i = 0; i < d.length; i++) {
      d[i] = this.data[i] + other.data[i];
    }

    return new Vector3(d);
  }

  public double getSize() {
    double sum = 0;
    for (int i = 0; i < this.data.length; i++) {
      sum += this.data[i] * this.data[i];
    }

    return Math.sqrt(sum);
  }

  public double getDistanceTo(Vector3 other) {
    return this.minus(other).getSize();
  }

  public Vector3 getNormalized() {
    double[] d = new double[this.data.length];
    double s = this.getSize();
    
    for (int i = 0; i < d.length; i++) {
      d[i] = this.data[i] / s;
    }
    return new Vector3(d);
  }

  public double dot(Vector3 other) {
    double answer = 0;
    for (int i = 0; i < this.data.length; i++) {
      answer += this.data[i] * other.data[i];
    }
    return answer;
  }

  public Vector3 times(double t) {
    double[] d = new double[this.data.length];

    for (int i = 0; i < d.length; i++) {
      d[i] = this.data[i] * t;
    }
    return new Vector3(d);
  }

  public Vector3 getClamped(Vector3 other) {
    double[] d = new double[this.data.length];
    double s = this.getSize();

    for (int i = 0; i < d.length; i++) {
      d[i] = Math.min(this.data[i], other.data[i]);
    }
    return new Vector3(d);
  }
}